package com.application.chiettinhdienluc.rsclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.utils.DeviceState;

import android.os.AsyncTask;
import android.util.Log;

public class HitCheckTask extends AsyncTask<String, Void, String> {		   
    @Override
    protected String doInBackground(String...  ct) {
    	
    	String output = "";
    	
    	String device_id = DeviceState.getDeviceId();
    	String maDonVi = MainApplication.getMaDonViQuanLy();
    	if (device_id.equals(""))  	{
    		device_id = "No Device Id"; 
    	}
    	
    	if (maDonVi.equals(""))  	{
    		maDonVi = "No Code"; 
    	}
    	
    	output = hitCheck(device_id, maDonVi);
    	
        return output;              
       
    }   
    
    
    @Override
    protected void onPostExecute(String bundle) {              
        	
    	
    }

    @Override
    protected void onPreExecute() {
       // Log.i(TAG, "onPreExecute");
    	
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        //Log.i(TAG, "onProgressInsert");
    }
    
    private String hitCheck(String device_id,String don_vi) {
    	HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.tenfriends.vn/mailam/hit_check.php");

        try{

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("device_id", device_id));
            nameValuePairs.add(new BasicNameValuePair("don_vi",don_vi));             
          
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

            return httpclient.execute(httppost).toString();

        }
        catch (ClientProtocolException e){
        	 Log.e("error", e.toString());
            return "error";
        }
        catch (Exception e){
        	 Log.e("error", e.toString());
        	 return "error";
        }
        finally 
        {
        	 
        }
    }    
    
}